//
//  CZComposeTextView.swift
//  weibo
//
//  Created by 丁蒙 on 16/7/3.
//  Copyright © 2016年 丁蒙. All rights reserved.
//

import UIKit

class CZComposeTextView: UITextView {
    //重写父类方法 设置字体
    override var font: UIFont? {
        didSet {
            placehoderLabel.font = font
        }
    }
    //赋值
    var placeholder: String? {
        didSet {
            placehoderLabel.text = placeholder
        }
    }

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 设置视图
    private func setupUI() {
        self.backgroundColor = UIColor.whiteColor()
        //添加子控件
        addSubview(placehoderLabel)
        
        //添加约束
        placehoderLabel.snp_makeConstraints { (make) in
            make.left.equalTo(self).offset(5)
            make.top.equalTo(self).offset(8)
            make.width.equalTo(SCREENW - 10)
        }
        
        // MARK: - 通过系统通知监听textView文字变化
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CZComposeTextView.textViewTextDidChange), name: UITextViewTextDidChangeNotification, object: nil)
    }
    // MARK: - 监听通知的方法
    func textViewTextDidChange() {
        self.placehoderLabel.hidden = self.hasText()
    }
    // MARK: - 移除通知
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    // MARK: - 懒加载控件
    private lazy var placehoderLabel: UILabel = {
       let lab = UILabel()
        lab.textColor = UIColor.grayColor()
        lab.font = UIFont.systemFontOfSize(13)
        lab.numberOfLines = 0
        
        return lab
    }()

}
